package com.example.seckill.common.RabbitMQ;


import com.example.seckill.Util.JsonUtil;
import com.example.seckill.VO.BedVO;
import com.example.seckill.VO.OrderRequest;
import com.example.seckill.VO.RoomVO;
import com.example.seckill.VO.Student_Bed_Order_VO;
import com.example.seckill.mapper.BedMapper;
import com.example.seckill.mapper.OrderMapper;
import com.example.seckill.mapper.RoomMapper;
import com.example.seckill.pojo.Bed;
import com.example.seckill.pojo.Student_Bed_Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@Slf4j
public class MQReceiver {
    @Autowired
    RoomMapper roomMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    BedMapper bedMapper;
//    @RabbitListener(queues="seckillQueue")
//    public void receive(Message msg, Channel channel) throws IOException {
//
//        System.out.println("接收到"+msg);
//        channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
//    }

    /**接收到信息进行下单操作**/
       @RabbitListener(queues="seckillQueue")
    public void receiveOrderMessage(String msg) throws IOException {

        System.out.println("接收到的消息"+msg);
        System.out.println(1);
        OrderRequest order= JsonUtil.jsonStr2Object(msg,OrderRequest.class);
        String rid=order.getRoom_id();
        String student_id=order.getStudent_id();
//        System.out.println(2);

        //判断库存
           RoomVO roomVO=roomMapper.GetRoomByRoomID(rid);
//           System.out.println("inventory="+roomVO.getRoom_inventory());
            if(roomVO.getRoom_inventory()<1) {
               System.out.println("mqsender判断库存小于1");
               redisTemplate.opsForValue().set("isEmptyStock:"+rid,"0");
                return ;
           }
           System.out.println(3);
           //判断重复抢购
           Student_Bed_Order foundOrder=(Student_Bed_Order) redisTemplate.opsForValue().get("order:"+student_id);
           if(foundOrder!=null) {
               return;
           }
           System.out.println(4);

           System.out.println("分配床位中");
           System.out.println("rid:"+rid);
           Bed selectedBed= bedMapper.DistributeBedByRID(rid);
           if(selectedBed==null)
           {  System.out.println("查找床位结果为空");
               System.out.println("没有床位");
               //没有床位是库存不足
               redisTemplate.opsForValue().set("isEmptyStock:"+rid,"0");
               return ;
           }
//           System.out.println("设置状态");
           Long bed_id=selectedBed.getId();
           selectedBed.setStatus(2);


           //减库存
           roomMapper.StockMinusOne(rid);
           System.out.println(5);
           //分配床位


           //生成订单
           System.out.println("生成订单中");
           Student_Bed_Order student_bed_order=new Student_Bed_Order();
           student_bed_order.setBed_id(bed_id);
           student_bed_order.setStudent_id(student_id);
           orderMapper.createNewStudentBedOrder(student_bed_order);
           BedVO bedVO=new BedVO();
           bedMapper.changeStatus(selectedBed);

//           orderMapper.createNewOrder(order);
           System.out.println("mqsender生成订单");
           redisTemplate.opsForValue().set("order:"+student_id,student_bed_order);
           System.out.println("mqsender生成订单成功");

    }
}
